package com.harmontronics.app.pedestal.bridge.model;

import android.os.Environment;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.luck.picture.lib.tools.SdkVersionUtils;

import java.io.File;

public class BaseBridge {
    protected String success(Object data) {
        BridgeResult result = new BridgeResult();
        result.data = data;
        String resultJson = result.toJson();
        LogUtils.d("success result: " + resultJson);
        return resultJson;
    }

    protected String fail(String message, Throwable e) {
        BridgeResult result = new BridgeResult();
        result.code = BridgeResult.CODE_FAIL;
        result.message = message;
        result.error = null != e && null != e.getMessage() ? e.toString() : "";
        String resultJson = result.toJson();
        LogUtils.file(LogUtils.E, resultJson);
        return resultJson;
    }

    /**
     * 创建自定义拍照输出目录
     *
     * @return
     */
    protected String createCustomCameraOutPath(boolean isImage) {
        File customFile;
        if (SdkVersionUtils.checkedAndroid_Q()) {
            // 在Android Q上不能直接使用外部存储目录；且沙盒内的资源是无法通过PictureSelector扫描出来的
            File externalPictureFilesDir = Utils.getApp().getExternalFilesDir(isImage ? Environment.DIRECTORY_PICTURES : Environment.DIRECTORY_MOVIES);
            customFile = new File(externalPictureFilesDir.getAbsolutePath(), "PictureSelector");
            if (!customFile.exists()) {
                customFile.mkdirs();
            }
        } else {
            File rootFile = Environment.getExternalStorageDirectory();
            customFile = new File(rootFile.getAbsolutePath() + File.separator + "CustomPictureCamera");
            if (!customFile.exists()) {
                customFile.mkdirs();
            }
        }
        return customFile.getAbsolutePath() + File.separator;
    }
}
